package com.shenda.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shenda.device.Options;
import com.shenda.device.message.ReqMsg;
import com.shenda.device.message.ReqMsg_GetMac;
import com.shenda.device.message.RespMsg;
import com.shenda.device.message.RespMsg_GetMac;
import com.shenda.device.message.RespMsg_UploadData;
import com.swak.rxtx.channel.Channel;

/**
 * 日志持有
 * 
 * @author lifeng
 */
public class LogHolder {

	/**
	 * 设备日志
	 */
	static Logger logger = LoggerFactory.getLogger(Options.class);

	/**
	 * 设备日志
	 */
	static Logger channel = LoggerFactory.getLogger(Channel.class);

	/**
	 * 请求日志
	 * 
	 * @param comm
	 * @param mac
	 * @param cmd
	 * @param content
	 */
	public static void log(ReqMsg req) {

		/**
		 * 不需要记录此命令
		 */
		if (req == null || req instanceof ReqMsg_GetMac) {
			return;
		}

		/**
		 * 处理响应的日志
		 */
		String comm = req.getDevice().getChannel().comm();
		String mac = req.getDevice().getMac();
		String cmd = req.getCmd().getName();
		String content = req.getContent();

		/**
		 * 通道的日志
		 */
		if (channel.isDebugEnabled()) {
			channel.debug("发送命令：{}\t{}\t{}\t{}", comm, mac, cmd, content);
		}

		/**
		 * 处理的日志
		 */
		if (logger.isInfoEnabled()) {
			logger.info("{}\t{}\t{}\t{}\t{}", comm, mac, cmd, content, "请求");
		}
	}

	/**
	 * 响应日志
	 * 
	 * @param comm
	 * @param mac
	 * @param cmd
	 * @param content
	 */
	public static void log(RespMsg resp) {

		/**
		 * 不需要记录此命令
		 */
		if (resp == null || resp instanceof RespMsg_GetMac || resp instanceof RespMsg_UploadData) {
			return;
		}

		/**
		 * 处理响应的日志
		 */
		String comm = resp.getDevice().getChannel().comm();
		String mac = resp.getDevice().getMac();
		String cmd = resp.getCmd().getName();
		String content = resp.getContent();

		/**
		 * 通道的日志
		 */
		if (channel.isDebugEnabled()) {
			channel.debug("处理命令：{}\t{}\t{}\t{}", comm, mac, cmd, content);
		}

		/**
		 * 处理的日志
		 */
		if (logger.isInfoEnabled()) {
			logger.info("{}\t{}\t{}\t{}\t{}", comm, mac, cmd, content, "响应");
		}
	}
}
